package com.evolve.autotest;

import java.util.LinkedList;
import java.util.List;


public class Test {

	private List<Statement> statements;
	private String testCaseName;
	private ReturnValue testResult;
	private List<String> troubles;

	public Test() {
		statements = new LinkedList<Statement>();
		troubles = new LinkedList<String>();
		testResult = ReturnValue.OK;
	}
	
	public String getName() {
		return testCaseName;
	}

	public void setName(String testCaseName) {
		this.testCaseName = testCaseName;
	}
	
	/**
	 * Gets the test result
	 * @return
	 */
	public ReturnValue getTestResult() {
		return testResult;
	}
	
	public void addStatement(Statement statement) {
		statements.add(statement);
	}
	
	public int numberOfStatements() {
		return statements.size();
	}

	/**
	 * Execute the action
	 * @return The return value of the action
	 */
	public ReturnValue execute() {
		
		ReturnValue statementReturnValue;
		
		for (Statement statement : statements) {
			
			statementReturnValue = statement.execute();
			
			// Check if the action should be verified
			if (statement.getVerification() != VerificationType.GO_ON) {
				troubles.addAll(statement.getTroubles());
			} 
			
			if (statementReturnValue.isMoreSevere(testResult)) {
				testResult = statementReturnValue;
			}
			
			if (testResult != ReturnValue.OK) {
				return testResult;
			}
		}
		
		return testResult;
	}
	
	/**
	 * Gets the list of troubles
	 * @return
	 */
	public List<String> getTroubles() {
		return troubles;
	}
}
